﻿using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Widget;
using AndroidX.AppCompat.App;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using WSYmi.MainAct;
using Xamarin.Essentials;
using AlertDialog = AndroidX.AppCompat.App.AlertDialog;

namespace WSYmi.TableAct
{
    [Activity(Label = "TableActivity", LaunchMode = LaunchMode.SingleTop)]
    public class TableActivity : AppCompatActivity
    {
        /// <summary>
        /// ListView定制适配器
        /// </summary>
        public static TableAdapter TableAdapter { get; set; }

        private CsvFileInfo FileInfo { get; set; }

        protected override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_table);
            // Create your application here

            //string fileName = Intent.GetStringExtra("FileInfo");
            FileInfo = JsonConvert.DeserializeObject<CsvFileInfo>(Intent.GetStringExtra("FileInfo"));
            string fileName = FileInfo.FileName;
            List<string> rows = await MainAct.FileData.ReadTextAsync(fileName);
            List<List<string>> data = new List<List<string>>();
            foreach (var item in rows)
            {
                List<string> list = new List<string>();
                foreach (var str in item.Split(','))
                {
                    list.Add(str);
                }
                data.Add(list);
            }

            //设置ListView控件
            ListView listView = FindViewById<ListView>(Resource.Id.Ltv气象数据);
            TableAdapter = new TableAdapter(this, data);
            listView.Adapter = TableAdapter;

            TextView tv文件名 = FindViewById<TextView>(Resource.Id.Tv文件名);
            tv文件名.Text = $"{FileInfo.Name}.csv";

            FindViewById<TextView>(Resource.Id.Tv表格页面标题).Click += (sender, e) =>
            {
                Finish();
            };

            FindViewById<Button>(Resource.Id.Btn删除).Click += Btn删除_Click;
            FindViewById<Button>(Resource.Id.Btn分享).Click += Btn分享_Click;
        }

        private async void Btn分享_Click(object sender, EventArgs e)
        {
            var srcFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), FileInfo.FileName);
            var disFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), FileInfo.Name + ".csv");
            File.Copy(srcFile, disFile, true);
            var sharefile = new ShareFile(disFile)
            {
                ContentType = "text/plain"
            };
            await Share.RequestAsync(new ShareFileRequest
            {
                Title = $"分享环境数据文件{disFile}",
                File = sharefile,
            });
            File.Delete(disFile);
        }

        private void Btn删除_Click(object sender, EventArgs e)
        {
            var ad = new AlertDialog.Builder(this);
            ad.SetTitle($"删除文件[{FileInfo.Name}.csv]吗");
            ad.SetNegativeButton("取消", (sender, args) => { });
            ad.SetPositiveButton("删除", (sender, args) =>
            {
                var backingFile = Path.Combine(System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal), FileInfo.FileName);

                System.IO.File.Delete(backingFile);
                Intent intent = new Intent();
                intent.PutExtra("pathFileName", FileInfo.FileName);
                SetResult(Result.Ok, intent);//返回MainActivity
                Finish();
            });
            ad.Show();
        }
    }
}